<template>
  <div class="home">
    <el-table :data="blogData" style="width: 100%">
      <el-table-column type="expand">
        <template #default="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="Abstract:">
              <span>{{ props.row.abstract }}</span>
            </el-form-item>
            <el-form-item label="Tag:">
              <TagsWrapper :tags="props.row.tags" />
            </el-form-item>
            <el-form-item label="Topimg:">
              <el-image :src="props.row.topImg" fit="scale-down" />
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="blog ID" prop="id" />
      <el-table-column label="title" prop="title" />
      <el-table-column label="contentId" prop="contentId" />
      <el-table-column label="operation">
        <template #default="scope">
          <el-button size="mini" @click="handleEdit(scope.row.contentId)"
            >Edit</el-button
          >
          <el-popconfirm
            title="你确定要删除这篇文章吗?"
            @confirm="handleDelete(scope.row.contentId, getDataFunc)"
          >
            <template #reference>
              <el-button type="danger" size="mini">delete</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="home-pagination__box">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="count"
    >
    </el-pagination>
  </div>
</template>

<script>
import { useRouter } from "vue-router";
import { ref } from "vue";
import TagsWrapper from "@/components/TagsWrapper";
import { useInitEffect, useDeleteEffect, useEditEffect } from "./home-hooks";
export default {
  components: {
    TagsWrapper,
  },
  setup() {
    const pageSize = ref(5);
    const currentPage = ref(1);
    const router = useRouter();
    const { handleDelete } = useDeleteEffect();
    const { handleEdit } = useEditEffect(router);
    const { blogData, count, getDataFunc } = useInitEffect();
    const handleCurrentChange = (val) => {
      currentPage.value = val;
      getDataFunc({ currentPage: val, pageSize: pageSize.value });
    };
    const handleSizeChange = (val) => {
      pageSize.value = val;
      getDataFunc({ currentPage: currentPage.value, pageSize: val });
    };
    return {
      blogData,
      getDataFunc,
      count,
      handleDelete,
      handleEdit,
      handleCurrentChange,
      handleSizeChange,
      pageSize,
      currentPage,
    };
  },
};
</script>

<style scoped>
.home {
  min-height: 50%;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.home-pagination__box {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>